/**
 * Created by wzq on 2014/7/7.
 */
// 常量
var colorAlive = '#00ff00',
    colorDead = '#ffffff',
    colorLine = '#3c3c3c',
    lineWidth = 1,
    gridRowNum = 20,
    gridColNum = 20,
    deltaTime = 400;
//    aliveMax = 4,
//    aliveMin = 2;

var cGrid = document.getElementById('canvas_grid'),
    ctx = cGrid.getContext('2d'),
    gridHeight = cGrid.height / gridRowNum,
    gridWidth = cGrid.width / gridColNum;

ctx.strokeStyle = colorLine;
ctx.lineWidth = lineWidth;

var arrDoa = []; //array of dead Or Alive


initRdm();
//console.log("gridHeight=" + gridHeight + ", gridWidth=" + gridWidth);
refresh();

function refresh() {
    paintDoa();
    drawLines();
}

// init myArr check the array
function initRdm() {
    for (var i = 0; i < gridRowNum; i++) {
        var rowArr = Array(gridColNum)
        for (var j = 0; j < gridColNum; j++) {
            rowArr[j] = Number((Math.random() >= 0.5));
        }
        arrDoa[i] = rowArr;
    }
}

// 根据 doa array 绘制canvas中的小方格
function paintDoa() {
    for (var row = 0; row < gridRowNum; row++) {
        for (var col = 0; col < gridColNum; col++) {
            var isAlive = arrDoa[row][col];
            if (arguments[2]) {
                isAlive = arguments[2];
            }
            var left = col * gridWidth,
                top = row * gridHeight;
            if (isAlive) {
                ctx.fillStyle = colorAlive;
            } else {
                ctx.fillStyle = colorDead;
            }
            ctx.fillRect(left, top, gridWidth, gridHeight);
        }
    }
}

function drawLines() {
    // 画横线
    for (var i = 0; i < gridRowNum; i++) {
        var fromLeft = 0,
            toLeft = cGrid.width,
            fromHeight = i * gridHeight;
        ctx.moveTo(fromLeft, fromHeight);
        ctx.lineTo(toLeft, fromHeight);
    }

    // 画纵线
    for (var j = 0; j < gridColNum; j++) {
        var fromTop = 0,
            toTop = cGrid.height,
            fromLeft = j * gridWidth;
        ctx.moveTo(fromLeft, fromTop);
        ctx.lineTo(fromLeft, toTop);
    }
    ctx.stroke();
}

// 启动演变
var timeId;
function start() {
    timeId = setInterval(function () {
        evolution();
        refresh();
    }, deltaTime);
}

// 停止演变
function stop() {
    clearInterval(timeId);
}

// 演化

function evolution() {
    // copy before:
//    var arrDoaPreFrame = []; //前一帧记录
//    for (var i = 0; i < gridRowNum; i++) {
//        arrDoaPreFrame[i] = [];
//        for (var j = 0; j < gridColNum; j++) {
//            arrDoaPreFrame[i][j] = arrDoa[i][j];
//        }
//    }

    for (var m = 0; m < gridRowNum; m++) {
        for (var n = 0; n < gridColNum; n++) {
            var count = aroundCount(arrDoa, m, n);

            if (count == 3) {
                arrDoa[m][n] = 1;
            } else if (count == 2) {
                // keep
            } else {
                arrDoa[m][n] = 0;
            }
        }
    }
}

function aroundCount(arr, row, col) {
    var aroundAlive = 0;
    for (var i = row - 1; i <= row + 1; i++) {
        for (var j = col - 1; j <= col + 1; j++) {
            if (arr[i] && arr[i][j]) {
                aroundAlive += 1;
            }
        }
    }

    aroundAlive -= arr[row][col];
    return aroundAlive;
}

// 改变一个方格的状态
function click(event) {
    console.log('click');
    var row = parseInt(event.layerY / gridHeight, 10);
    var col = parseInt(event.layerX / gridWidth, 10);

    if (arrDoa[row][col]) {
        arrDoa[row][col] = 0;
    } else {
        arrDoa[row][col] = 1;
    }
    refresh();
}